package com.zhilifang.modules.zlf.utils;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;


public class Sitemap {
	
	static String locPrefix = "http://www.m3games.com";		//网站页面后缀
	
	static String locArray[] = new String[] {"/index","/ztgy","/hdzlg","/dmthy","/hddmt","/etly","/hdcy","/zthd","/jdal","/cpzx","/zxzx","/gywm"};		//网站页面前缀

	
	/*
	 * 在项目根目录下生成xml文件，百度地图爬虫获取
	 */
	public static void createXML(String fileAdd){
		Element urlset = new Element("urlset"); 
		Document document = new Document(urlset);
		//网页所有的链接集合
		List<com.zhilifang.modules.zlf.entity.Sitemap>  list = new ArrayList<com.zhilifang.modules.zlf.entity.Sitemap>();
    	for(String locSuffix:locArray){
    		list.addAll(Urls.runs(locPrefix,locSuffix));
    	}
		for (com.zhilifang.modules.zlf.entity.Sitemap str : list) {
			//<!--必填标签,这是具体某一个链接的定义入口，每一条数据都要用<url>和</url>包含在里面，这是必须的 -->
			Element url = new Element("url");
			//<!--必填,URL链接地址,长度不得超过256字节-->
			Element loc = new Element("loc");
			loc.setText(str.getLoc());
			url.addContent(loc);
			//<!--可以不提交该标签,用来指定该链接的最后更新时间-->
			Element lastmod = new Element("lastmod");
			lastmod.setText(str.getLastmod());
			url.addContent(lastmod);
			//<!--可以不提交该标签,用这个标签告诉此链接可能会出现的更新频率 -->
			Element changefreq = new Element("changefreq");
			changefreq.setText(str.getChangefreq());
			url.addContent(changefreq);
			//<!--可以不提交该标签,用来指定此链接相对于其他链接的优先权比值，此值定于0.0-1.0之间-->
			Element priority = new Element("priority");
			priority.setText(str.getPriority());
			url.addContent(priority);
			urlset.addContent(url);
		}
		System.out.println("生成中...");
		XMLOutputter XMLOut = new XMLOutputter();  
    	try {
    		Format f = Format.getPrettyFormat();  
    		f.setEncoding("UTF-8");//default=UTF-8
    		XMLOut.setFormat(f); 
    		
    		String path = fileAdd+"/sitemap.xml";
    		XMLOut.output(document, new FileOutputStream(path));
    	} catch (Exception e) {  
    		e.printStackTrace();  
    	}
	}
	
	
	/*
	 * 返回生成html文件的数据，前台展示
	 */
	public static List<com.zhilifang.modules.zlf.entity.Sitemap> createHTML(){
		//网页所有的链接集合
		List<com.zhilifang.modules.zlf.entity.Sitemap>  list = new ArrayList<com.zhilifang.modules.zlf.entity.Sitemap>();
    	for(String locSuffix:locArray){
    		list.addAll(Urls.runs(locPrefix,locSuffix));
    	}
    	return list;
	}

}
